package cn.iocoder.yudao.hires.service.impl;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.hires.service.DictSchoolService;
import cn.iocoder.yudao.hires.vo.DictSchoolPageReqVO;
import cn.iocoder.yudao.hires.vo.DictSchoolSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.hires.entity.DictSchoolDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.hires.mapper.DictSchoolMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 高校字典 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DictSchoolServiceImpl implements DictSchoolService {

    @Resource
    private DictSchoolMapper dictSchoolMapper;

    @Override
    public Integer createDictSchool(DictSchoolSaveReqVO createReqVO) {
        // 插入
        DictSchoolDO dictSchool = BeanUtils.toBean(createReqVO, DictSchoolDO.class);
        dictSchoolMapper.insert(dictSchool);
        // 返回
        return dictSchool.getId();
    }

    @Override
    public void updateDictSchool(DictSchoolSaveReqVO updateReqVO) {
        // 校验存在
        validateDictSchoolExists(updateReqVO.getId());
        // 更新
        DictSchoolDO updateObj = BeanUtils.toBean(updateReqVO, DictSchoolDO.class);
        dictSchoolMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictSchool(Integer id) {
        // 校验存在
        validateDictSchoolExists(id);
        // 删除
        dictSchoolMapper.deleteById(id);
    }

    private void validateDictSchoolExists(Integer id) {
        if (dictSchoolMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500, "高校字典不存在"));
        }
    }

    @Override
    public DictSchoolDO getDictSchool(Integer id) {
        return dictSchoolMapper.selectById(id);
    }

    @Override
    public PageResult<DictSchoolDO> getDictSchoolPage(DictSchoolPageReqVO pageReqVO) {
        return dictSchoolMapper.selectPage(pageReqVO);
    }

}